/** $Id: GridSizeModel.java 63 2010-02-14 13:52:34Z aaron.day $ */
package org.crazydays.gameplan.map.swing;


import java.util.LinkedList;
import java.util.List;

import javax.swing.SpinnerModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.crazydays.gameplan.map.Geometry;

/**
 * GridSizeSpinnerModel
 */
public class GridSizeModel
    implements SpinnerModel
{
    /** geometry */
    protected Geometry geometry;

    /** change listeners */
    protected List<ChangeListener> listeners = new LinkedList<ChangeListener>();

    /**
     * GridSizeSpinnerModel constructor.
     * 
     * @param geometry Geometry
     */
    public GridSizeModel(Geometry geometry)
    {
        this.geometry = geometry;
    }

    /**
     * Get next value.
     * 
     * @return Value
     * @see javax.swing.SpinnerModel#getNextValue()
     */
    @Override
    public Object getNextValue()
    {
        return geometry.getSize() + 1;
    }

    /**
     * Get previous value.
     * 
     * @return Value
     * @see javax.swing.SpinnerModel#getPreviousValue()
     */
    @Override
    public Object getPreviousValue()
    {
        return geometry.getSize() - 1;
    }

    /**
     * Get current size.
     * 
     * @return Size
     * @see javax.swing.SpinnerModel#getValue()
     */
    @Override
    public Object getValue()
    {
        return geometry.getSize();
    }

    /**
     * Set value.
     * 
     * @param value Value
     * @see javax.swing.SpinnerModel#setValue(java.lang.Object)
     */
    @Override
    public void setValue(Object value)
    {
        this.geometry.setWidth((Integer) value);

        for (ChangeListener listener : listeners) {
            listener.stateChanged(new ChangeEvent(geometry));
        }
    }

    /**
     * @param listener Listener
     * @see javax.swing.SpinnerModel#addChangeListener(javax.swing.event.ChangeListener)
     */
    @Override
    public void addChangeListener(ChangeListener listener)
    {
        listeners.add(listener);
    }

    /**
     * @param listener Listener
     * @see javax.swing.SpinnerModel#removeChangeListener(javax.swing.event.ChangeListener)
     */
    @Override
    public void removeChangeListener(ChangeListener listener)
    {
        listeners.remove(listener);
    }
}
